package data;
import java.sql.*;
import java.util.List;
import java.util.LinkedList;
 

import domain.Donacion;
import domain.Donador;



public class JDBCDonacionDAO implements IDonacionDAO{

	private Connection conn;
    IDonadorDAO ddao;
    
     public JDBCDonacionDAO() {
            conn = ConnectionManager.getInstance().checkOut();
            ddao = new JDBCDonadorDAO();
           
        }

       // protected void finalize() {
         //   ConnectionManager.getInstance().checkIn(conn);
           // }
        
       /* private String selectOIDOfDonador(String donacionOID) {
            PreparedStatement stmt = null;
            ResultSet result = null;
            String oidDonacion = null;
            String sql = "SELECT * FROM Donaciones WHERE (OID = ? ) ";

            try {
                stmt = conn.prepareStatement(sql);
                stmt.setString(1, donacionOID);

                result = stmt.executeQuery();

                result.next();
                oidDonacion = result.getString("DonacionOID");
            } catch (SQLException e) {
                System.out.println("Message: " + e.getMessage());
                System.out.println("SQLState: " + e.getSQLState());
                System.out.println("ErrorCode: " + e.getErrorCode());
            } finally {
                try {
                    if (result != null) {
                        result.close();
                    }

                    if (stmt != null) {
                        stmt.close();
                    }
                } catch (SQLException e) {
                }
            }

            return oidDonacion;
        }
    */
    
   
    public void insertDonacion(Connection conn, Donacion d) {
            // TODO Auto-generated method stub
            
    }

   
    public List<Donacion> selectAllDonaciones() {
            // TODO Auto-generated method stub
            return null;
    }

    
    public List<Donacion> selecAllDonacionesPendientes() {
            // TODO Auto-generated method stub
            PreparedStatement stmt = null;
    List<Donacion> searchResults = new LinkedList<Donacion>();
    ResultSet result = null;
    
    try {

        String sql = "SELECT * FROM Donaciones WHERE pendiente = true";
        stmt = conn.prepareStatement(sql); //conn es una conexión válida
        stmt.executeQuery();
        result = stmt.executeQuery();
            
        while (result.next()) {
            Donacion temp = new Donacion();
            //get Donador
            Donador dona=ddao.select(conn, result.getString("DonadorOID"));
            temp.setDonador(dona);
            
            
            //getArticulos
            temp.setArticulosDonados(result.getString("Articulos"));
            //getPendiente
            temp.setPendiente(result.getBoolean("Pendiente"));
            
            searchResults.add(temp);
        }
    } catch (SQLException e) {
        System.out.println("Message: " + e.getMessage());
        System.out.println("SQLState: " + e.getSQLState());
        System.out.println("ErrorCode: " + e.getErrorCode());
    } finally {
        ConnectionManager.getInstance().checkIn(conn);
        try {
            if (result != null)
                result.close();
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
        }

    }
    return searchResults;
    }


	public boolean updateDonacion(Connection conn, Donacion d) {
		// TODO Auto-generated method stub
		
		
		return false;
	}

	public Donacion select(Connection conn, String donacionOID) {
		// TODO Auto-generated method stub
		return null;
	}

	

	

	
}
